package java6681.javafx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


/*
* 套娃：Stage --> Scene --> VBox
* */

public class EventDemo extends Application {
    private Label label;

    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox root = new VBox(10);
        Button button = new Button("点我");
        label = new Label("Hello");
        root.getChildren().addAll(label,button);
        root.setAlignment(Pos.CENTER);

        Scene scene = new Scene(root, 300, 300);

        primaryStage.setScene(scene);
        primaryStage.setTitle("事件处理");
        primaryStage.show();

        EventHandler<ActionEvent> listener = new MyEvenHandler();
        button.setOnAction(listener);

    }
//      监听器
    class MyEvenHandler implements EventHandler<ActionEvent>{

        @Override
        public void handle(ActionEvent event) {
            if (label.getText().equals("Hello")){
                label.setText("您好");
            }else{
                label.setText("Hello");
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

